/**
 * @description 接口服务模拟
 * @author generate by qdp-cli 0.1.11
 * @date 2020-6-3 11:12:22
 * @version 1.0
 */
import MockAdapter from 'axios-mock-adapter';
import Mocker from 'mockjs';

export default (axios) => {
  const mock = new MockAdapter(axios);
  mock.onPost(/create|modify|update|remove|export|disable|enable/).reply(() => [200, Mocker.mock({ ret: 0 })]);

  const buffers = [
    {
      address: 'permissions',
      paper: true,
      entity: {
        "id": "@guid",
        "createTime": 1572665875942,
        "lastModifyTime": 1572665875942,
        "name": "@name",
        "remark": "@cparagraph"
      }
    },
    {
      address: 'user',
      paper: true,
      entity: {
        "id": "@guid",
        "createTime": 1572665875942,
        "lastModifyTime": 1572665875942,
        "userName": "@name",
        "nickName": "@cname",
        "permissionId": "@guid",
        "permissionName": "@csentence(5,10)",
        "remark": "@cparagraph"
      }
    }
  ];

  buffers.forEach(p => {
    const pageInfo = {
      "pageCount": 10,
      "pageNumber": 1,
      "pageSize": 10,
      "paginated": true,
      "recordCount": 100,
      "resultsAvailable": true
    }
    const { address, entity, paper } = p;
    if (paper) {
      mock.onGet(`${address}/query`).reply(() => [200, Mocker.mock({ ret: 0, "data": { ...pageInfo, "resultData|10": [entity] } })]);
    } else {
      mock.onGet(`${address}/query`).reply(() => [200, Mocker.mock({ ret: 0, "data|10": [entity] })]);
    }
    mock.onGet(`${address}/detail`).reply(() => [200, Mocker.mock({ ret: 0, data: entity })]);
  });

  mock.onPost('/passport/login')
    .reply(() => [200, Mocker.mock({ ret: 0, "data": { token: '@guid' } })]);

  mock.onPost('/passport/validate')
    .reply(() => [200, Mocker.mock({ "ret": 0, "data": { "id": "administrator", "nickName": "超级管理员", "permissionName": "超级管理员", "remark": "系统管理员，不受权限控制，不可删除", "token": "a648a86363554df6a0eb9caf428b782f", "userName": "admin" } })]);
}
